package upload

import (
	"context"
	"github.com/aws/aws-sdk-go/service/s3"
	"mime/multipart"
)

// Uploader 是一个用于上传文件的接口
type Uploader interface {
	Upload(file multipart.File, handler *multipart.FileHeader, provider string) (*UploadResult, error)
	GetUploadURL(fileName, provider string) (string, error)
}
type StorageClient interface {
	PutObjectWithContext(ctx context.Context, input *s3.PutObjectInput) (*s3.PutObjectOutput, error)
}

type ProviderData struct {
	Bucket   string
	Endpoint string
	Folder   string
	UseCdn   bool
	CdnUrl   string
	Id       uint64
}

type UploadResult struct {
	ID          string
	Name        string
	URL         string
	RelativeSrc string
	Size        uint64
	FileType    uint8
	UserID      string
}
type Config struct {
	MaxImageSize        int64  `json:",default=33554432,env=MAX_IMAGE_SIZE"`
	MaxVideoSize        int64  `json:",default=1073741824,env=MAX_VIDEO_SIZE"`
	MaxAudioSize        int64  `json:",default=33554432,env=MAX_AUDIO_SIZE"`
	MaxOtherSize        int64  `json:",default=10485760,env=MAX_OTHER_SIZE"`
	PrivateStorePath    string `json:",env=PRIVATE_PATH"`
	PublicStorePath     string `json:",env=PUBLIC_PATH"`
	ServerURL           string `json:",env=SERVER_URL"`
	DeleteFileWithCloud bool   `json:",env=DELETE_FILE_WITH_CLOUD,default=true"`
	DefaultProvider     string
}

//// NewCloudUploaderFromDB 从数据库构建 CloudUploader 实例
//func NewCloudUploaderFromDB(db *ent.Client) *CloudUploader {
//	cloudService := cloud.NewCloudServiceGroup(db)
//
//	// 构建 storage map: string -> StorageClient（即 *S3Adapter）
//	storageClients := make(map[string]StorageClient)
//	for name, svc := range cloudService.CloudStorage {
//		storageClients[name] = &S3Adapter{Client: svc}
//	}
//
//	// 构建默认配置（可从 env 或 config 文件中读取）
//	cfg := &Config{
//		MaxImageSize:        10 << 20,  // 默认 10MB
//		MaxVideoSize:        100 << 20, // 默认 100MB
//		MaxAudioSize:        30 << 20,  // 默认 30MB
//		MaxOtherSize:        1 << 20,   // 默认 1MB
//		DefaultProvider:     cloudService.DefaultProvider,
//		PrivateStorePath:    "private",
//		PublicStorePath:     "public",
//		ServerURL:           "http://localhost:8080",
//		DeleteFileWithCloud: true,
//	}
//
//	return &CloudUploader{
//		config:   cfg,
//		storage:  storageClients,
//		provider: convertProviderData(cloudService.ProviderData),
//	}
//}
//
//// convertProviderData 将 cloud.ProviderData 转换为 upload.ProviderData
//func convertProviderData(data map[string]struct {
//	Id       uint64
//	Folder   string
//	Bucket   string
//	Endpoint string
//	UseCdn   bool
//	CdnUrl   string
//}) map[string]ProviderData {
//	result := make(map[string]ProviderData)
//	for k, v := range data {
//		result[k] = ProviderData{
//			Id:       v.Id,
//			Folder:   v.Folder,
//			Bucket:   v.Bucket,
//			Endpoint: v.Endpoint,
//			UseCdn:   v.UseCdn,
//			CdnUrl:   v.CdnUrl,
//		}
//	}
//	return result
//}
